Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services


<ToolboxData("<{0}:HTMLEditor runat=server></{0}:HTMLEditor>")> _
Public Class HTMLEditor
    Inherits jObjectiveControl

    Private m_ctlBoldButton As New Button
    Private m_ctlItalicButton As New Button
    Private m_ctlUnderlineButton As New Button
    Private m_ctlEditor As New HtmlControls.HtmlGenericControl("div")
    Private WithEvents m_ctlEditorText As New TextBox

    Public Event TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Public Property Text() As String
        Get
            Return HttpUtility.UrlDecode(m_ctlEditorText.Text)
        End Get
        Set(ByVal value As String)
            m_ctlEditorText.Text = HttpUtility.UrlEncode(value)
            m_ctlEditor.InnerHtml = value
        End Set
    End Property

    Public Property [ReadOnly]() As Boolean
        Get
            Return (m_ctlEditor.Attributes("contenteditable") = "false")
        End Get
        Set(ByVal value As Boolean)
            m_ctlEditor.Attributes("contenteditable") = (Not value).ToString().ToLower
        End Set
    End Property

    Public Sub New()
        MyBase.New()

        m_ctlEditor.ID = Common.GetUniqueID(Context)

        Constructor = "new jObjective.HTMLEditor($('#" & m_ctlEditor.ID & "'))"
    End Sub

    Protected Overrides Sub CreateChildControls()
        MyBase.CreateChildControls()

        With m_ctlBoldButton
            .CssClass = "Button Bold"
            .Text = ""
        End With

        With m_ctlItalicButton
            .CssClass = "Button Italic"
            .Text = ""
        End With

        With m_ctlUnderlineButton
            .CssClass = "Button Underline"
            .Text = ""
        End With

        With m_ctlEditor
            .Attributes("class") = "Editor"
        End With

        With m_ctlEditorText
            .Style("display") = "none"
            .ObjectName = "EditorText"
        End With

        With Controls
            .Add(m_ctlBoldButton)
            .Add(m_ctlItalicButton)
            .Add(m_ctlUnderlineButton)
            .Add(m_ctlEditor)
            .Add(m_ctlEditorText)
        End With

        ChildControlsCreated = True
    End Sub

    Private Sub HTMLEditor_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        If [ReadOnly] Then
            m_ctlBoldButton.Enabled = False
            m_ctlItalicButton.Enabled = False
            m_ctlUnderlineButton.Enabled = False

            With m_ctlEditor
                .Attributes.Remove("onblur")
            End With
        Else
            With m_ctlBoldButton
                .OnClientClick = "try{document.execCommand('bold',false,null);} catch(ex) {} return false;"
            End With

            With m_ctlItalicButton
                .OnClientClick = "try{document.execCommand('italic',false,null);} catch(ex) {} return false;"
            End With

            With m_ctlUnderlineButton
                .OnClientClick = "try{document.execCommand('underline',false,null);} catch(ex) {} return false;"
            End With

            With m_ctlEditor
                .Attributes("onblur") = [Namespace] & ".stripHTML();"
            End With
        End If
    End Sub

    Private Sub m_ctlEditorText_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_ctlEditorText.TextChanged
        RaiseEvent TextChanged(sender, e)
    End Sub
End Class